﻿

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using RsCms.Application.User.Dto;
using RsCode;
using RsCode.AspNetCore;
using RsCode.AspNetCore.Jwt;
using Rswl.Common.Application;
using Rswl.Common.Application.User.Dto;
using Rswl.Common.Core;

namespace Plugin.User.Controllers
{
   
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        IUserAppService userService;
        IUserDomainService userDomainService;
        public UserController(IUserAppService _userService,IUserDomainService userDomainService)
        {
            userService = _userService;
            this.userDomainService = userDomainService;
        }

       
        //手机注册
        [HttpPost("phone/register")]
        public async Task<AccessTokenInfo> PhoneRegisterAsync(PhoneRegisterDto dto)
        {
            return  await userService .PhoneRegisterAsync(dto);
        }

     

        //当前用户
        [EnableCors("CorsPolicy")]
        [Authorize]
        [Idempotent]
        [HttpGet("info")]
        public  async Task<object> GetLoginUserAsync(string appId)
        {
            return  await userService.GetLoginUserInfoAsync(appId);
        }

        
        //获取验证码
        [HttpGet("safecode")]
        public async Task<ActionResult> GetCodeAsync()
        {
            var safeCode = await userDomainService.GetSafeCodeImageAsync();
            return File(safeCode, @"image/png");
        }



        [HttpGet("list")]
        public async Task<object> GetUserListAsync(int page,int pageSize)
        {
           return await userService.PageUserInfoAsync(page, pageSize);

        }
        





        //使用第三方信息获取或创建用户
        [HttpPost("info")]
        public async Task<object> GetOrCreateUserAysnc(UserAuthInfoDto dto)
        {
            return await userService.GetOrCreateUserAsync(dto);
        }
         



        [Authorize]
        [HttpPost("info/change")]
        public async Task<object> ChangeUserInfoAsync(ChangeUserInfoDto dto)
        {
            var userId = User.Claims.FirstOrDefault(c => c.Type == "UserId")?.Value ?? "";
            return await userService.ChangeUserInfoAsync(userId, dto);
        }


 
        [Authorize]
        [HttpPost("setadmin/{userid}")]
        public async Task SetAdminAsync(string userId)
        {
            await userService.SetAdminAsync(userId);

        }
        [Authorize]
        [HttpPost("canceladmin/{userId}")]
        public async Task CancelAdminAsync(string userId)
        {
            await userService.CancelAdminAsync(userId);
        }

        [Authorize]
        [HttpGet("address")]
        public async Task<object> ShippingAddressAsync()
        {
            var userId = User.Claims.FirstOrDefault(c => c.Type == "UserId")?.Value ?? "";
            return await userService.GetShippingAddressesAsync(userId);
        }
        [Authorize]
        [HttpPost("address")]
        public async Task<object> SaveShippingAdressAsync(ShippingAddressEditDto dto)
        {
            var userId = User.Claims.FirstOrDefault(c => c.Type == "UserId")?.Value ?? "";
            return await userService.SaveShippingAddressAsync(userId, dto);
        }

        [Authorize]
        [HttpDelete("address")]
        public async Task<object> SaveShippingAdressAsync(long id)
        {
            var userId = User.Claims.FirstOrDefault(c => c.Type == "UserId")?.Value ?? "";
            await userService.RemoveShippingAddressAsync(userId, id);
            return "ok";
        }



      

        /// <summary>
        /// 绑定邀请码
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [Authorize]
        [Idempotent]
        [HttpPost("invitecode")]
        public async Task<object> BindInviteCodeAsync(BindInviteCodeDto dto)
        {
            var userId = User.Claims.FirstOrDefault(c => c.Type == "UserId")?.Value ?? "";
            await userService.BindInviteCodeAsync(userId, dto.InviteCode);
            return "ok";
        }

        /// <summary>
        /// 获取用户积分价格
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet("integral/price")]
        public async Task<object> GetIntegralPrice()
        {
            var userId = User.Claims.FirstOrDefault(c => c.Type == "UserId")?.Value ?? "";
            var user = await userService.GetUserAsync(userId);
            return userService.GetIntegralPriceInfo(user.Grade);
        }


        [Authorize]
        [Idempotent]
        [HttpGet("fans/list")]
        public async Task<object> GetFanListAsync(int page, int pageSize = 20)
        {
            var userId = User.Claims.FirstOrDefault(c => c.Type == "UserId")?.Value ?? "";
            return await userService.PageFansAsync(page, pageSize, userId);
        }


       

        [HttpPost("role")]
        public async Task<object> SaveRoleAsync(UserRoleEditDto dto)
        {
            await Task.Run(()=>userService.SaveUserRole(dto));
            return "ok";
        }

        [HttpDelete("role")]
        public async Task<object>DeleteUserRoleAsync(long userRoleId)
        {
            await Task.Run(()=>userService.DeleteUserRole(userRoleId));
            return "Ok";
        }
    }
}



